/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *		pub_sys.cpp
 * 位 置:
 *		src/biz/pub_sys.cpp
 * 描 述:
 *		公用系统类
 * 备 注:
 *		无
 * 修 改:
 *		2008-09-21  Simon   - 创建
 *
 **************************************************************/

#include <string>
#include <iostream>
#include <exception>
#include "occi.h"
#include "pub/log.h"
#include "pub/err.h"
#include "pub/util.h"
#include "biz/pub_sys.h"

using namespace std;
using namespace oracle::occi;

//----------------------------------------------
//	构造函数
//----------------------------------------------
PubSystem::PubSystem(Connection* conn)
{
	this->conn = conn;
}

//----------------------------------------------
//	析构函数
//----------------------------------------------
PubSystem::~PubSystem()
{
	this->conn = 0;
}

//----------------------------------------------
//	初始化系统
//----------------------------------------------
void PubSystem::init()
{
	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;

	stmt->setSQL(" \
		select paramValue \
		from SysParam \
		where upper(paramKey) = upper(:paramKey)");

	//获取系统状态
	try
	{
		Log::debug("获取系统状态...");
		stmt->setString(1, SYSPARAM_SYSSTAT);
		rs = stmt->executeQuery();
		rs->next();
		sysStatus = rs->getString(1);
		Log::debug("系统状态: [%s]", sysStatus.c_str());
	}
	catch (exception& e)
	{
		Log::error("获取系统状态出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}

	//获取系统工作日
	try
	{
		Log::debug("获取系统工作日...");
		stmt->setString(1, SYSPARAM_WORKDAY);
		rs = stmt->executeQuery();
		rs->next();
		tradeDate = rs->getString(1);
		Log::debug("系统工作日: [%s]", tradeDate.c_str());
	}
	catch (exception& e)
	{
		Log::error("获取系统工作日出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}

	//获取系统下一工作日
	try
	{
		Log::debug("获取系统下一工作日...");
		stmt->setString(1, SYSPARAM_NEXTWORKDAY);
		rs = stmt->executeQuery();
		rs->next();
		nextTradeDate = rs->getString(1);
		Log::debug("系统下一工作日: [%s]", nextTradeDate.c_str());
	}
	catch (exception& e)
	{
		Log::error("获取系统下一工作日出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}

	//获取销售商代码
	try
	{
		Log::debug("获取销售商代码...");
		stmt->setString(1, SYSPARAM_DISTRIBUTORCODE);
		rs = stmt->executeQuery();
		rs->next();
		distributorCode = rs->getString(1);
		Log::debug("销售商代码: [%s]", distributorCode.c_str());
	}
	catch (exception& e)
	{
		Log::error("获取销售商代码出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}

	//获取日志级别
	try
	{
		Log::debug("获取日志级别...");
		stmt->setString(1, SYSPARAM_LOGLEVEL);
		rs = stmt->executeQuery();
		rs->next();
		logLevel = rs->getInt(1);
		Log::debug("日志级别: [%d]", logLevel);
	}
	catch (exception& e)
	{
		Log::error("获取日志级别出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}

	//获取日志目标
	try
	{
		Log::debug("获取日志目标...");
		stmt->setString(1, SYSPARAM_LOGTO);
		rs = stmt->executeQuery();
		rs->next();
		logTo = rs->getInt(1);
		Log::debug("日志目标: [%d]", logTo);
	}
	catch (exception& e)
	{
		Log::error("获取日志目标出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}

	//获取日志文件
	try
	{
		Log::debug("获取日志文件...");
		stmt->setString(1, SYSPARAM_LOGFILE);
		rs = stmt->executeQuery();
		rs->next();
		logFile = rs->getString(1);
		Log::debug("日志文件: [%s]", logFile.c_str());
	}
	catch (exception& e)
	{
		Log::error("获取日志文件出错. %s", e.what());
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw;
	}

	stmt->closeResultSet(rs);
	conn->terminateStatement(stmt);

	//获取系统日期和时间
	sysdt(sysDate, sysTime);

	//获取交易时间
	tradeTime = sysTime;
}
